extends Node2D

var coin = 1000
var total_baozi_number = 0

func _ready():
	var file = File.new()
	if file.file_exists("user://data.json"):
		file.open("user://data.json",File.READ)
		var data = parse_json(file.get_as_text())
		coin = data.coin
		total_baozi_number = data.baozi
		$CoinLabel.text = "金币:" +str(coin)
		$Baozi.text = "包子:" + str(total_baozi_number)
	pass
	
func buy(obj,number):
	var cost = number * obj.cost
	if coin >= cost:
		coin -= cost
		total_baozi_number += number
		$CoinLabel.text = "金币:"+str(coin)
		$Baozi.text = "包子:"+str(total_baozi_number)

func _on_close_pressed():
	$Jinhuopanel.hide()
	pass # Replace with function body.

func _on_JinHuo_pressed():
	$Jinhuopanel.show()
	pass # Replace with function body.

func _on_buy_pressed():
	var number = int($Jinhuopanel/geshu.text)
	var item = BaoZi.new()
	buy(item,number)
	pass

func _on_Yinke_pressed():
	#$YinkeTimer.start()
	if total_baozi_number > 0:
		for i in 50:
			var guke = load("res://object/Guke.tscn").instance()
			guke.position = $Yinkepos.position
			yield(get_tree().create_timer(1.0),"timeout")
			add_child(guke)
	else:
		$JinHouTip.show()
		pass
	pass # Replace with function body.
	
func _on_Shop_body_entered(body):
	total_baozi_number -= 1
	$Baozi.text = "包子:" + str(total_baozi_number)
	coin += BaoZi.new().sell
	$CoinLabel.text = "金币:" + str(coin)
	body.queue_free()
	pass # Replace with function body.


func _on_ok_pressed():
	$JinHouTip.hide()
	pass # Replace with function body.


func _on_back_pressed():
	get_tree().change_scene("res://Main.tscn")
	pass # Replace with function body.


func _on_Shop_area_entered(area):
	if total_baozi_number > 0:
		total_baozi_number -= 1
		$Baozi.text = "包子:" + str(total_baozi_number)
		coin += BaoZi.new().sell
		$CoinLabel.text = "金币:" + str(coin)
	area.queue_free()
	pass # Replace with function body.


func _on_save_pressed():
	var data = to_json({"coin":coin,"baozi":total_baozi_number})
	var file = File.new()
	file.open("user://data.json",File.WRITE_READ)
	file.store_string(data)
	file.close()
	pass # Replace with function body.
